<?php
namespace Kuoqi\Html\Template;
/**
 * Class TablePaginate.class
 * @package Kuoqi\Html\Template
 * User kuoqi
 * Datetime 2022/3/7 15:56
 * Remark 常规分页列表
 */
class TablePaginate{

    //显示的列表数据
    protected $list_data = array();

    //显示的table-classname
    protected $table_classname = 'table table-toggle';

    //当前页面编码流
    protected $page_stream = '';

    //当前字段列表信息
    protected $columns = array();

    //数据列信息
    protected $stream = '';

    public function __construct($config)
    {
        if(isset($config['classname'])){ $this->table_classname = $config['classname'];}
        if(isset($config['dataProvider'])){  $this->list_data = $config['dataProvider'];}
        if(isset($config['pageStream'])){  $this->page_stream = $config['pageStream'];}
        if(isset($config['columns'])){  $this->columns = $config['columns'];}
    }



    //以dom数据加载进入
    public function toDomString(){
        //todo--显示信息
        $this->stream = '';
        $this->stream = sprintf('<table class="table table-toggle" border="none"><thead>%s</thead><tbody>%s</tbody></table>',
            $this->flushHead(),
            $this->flushBody()
            );
        return $this->stream;
    }


    //配置头部信息
    protected function flushHead(){
        $stream = '<tr>';
        foreach($this->columns as $item){
            $stream .= sprintf('<th><span>%s</span></th>',
                $item['attribute']
            );
        }
         $stream .= '</tr>';
        return $stream;
    }

    //配置列表信息
    protected function flushBody(){

        foreach ($this->list_data as $key=>$row){
            $stream = '<tr>';
            foreach($this->columns as $item){
                $stream .= sprintf('<td><span>%s</span></td>',
                    $item['value']($row)
                );
            }
            $stream .= '</tr>';
        }
        return $stream;
    }

}